clear
clear matrix
clear mata
program drop _all
set more off
set emptycells drop
set maxvar 100000

#delimit ;
cap log close;


#delimit cr

tempfile main


cd "<directory>"


*The code on lines 25-200 accomplishes the following:;
*	1. Create additional variables, some of which are never used in the analysis;
*	2. Create the POST indicator POST indicator;

u "<directory>",clear

egen maxliqsaleprice=max(liqsalesprice),by(dosname masterloanidtrepp)
drop liqsalesprice
ren maxliqsaleprice liqsalesprice
egen maxdisp=max(disposedamount),by(dosname masterloanidtrepp)
egen maxdate=max(distdate) if remterm!=.,by(dosname masterloanidtrepp)
gen unscheddate=distdate if curunschedprin!=0 & curunschedprin!=.
gen datediff=abs(maxdate-unscheddate)
egen mindiff=min(datediff),by(dosname masterloanidtrepp)
gen curunschedprin_keep=curunschedprin if datediff==mindiff
egen max_curunschedprin_keep=max(curunschedprin_keep),by(dosname masterloanidtrepp)
drop curunschedprin_keep
ren max_curunschedprin_keep curunschedprin_keep
sa `main',replace

tempfile multppdpo
gen indic2=1 if curunschedprin!=. & curunschedprin!=0
gen ddumind=1 if (firstprepaycd==6 | (lastworkoutmethod==6 & (firstprepaycd==3 | firstprepaycd==7)) )
egen maxddumind=max(ddumind),by(dosname masterloanidtrepp)
keep if maxddumind==1
drop maxddumind
egen allindic2=sum(indic2),by(dosname masterloanidtrepp)
order dosname masterloanidtrepp distdate curunschedprin curunschedprin_keep remterm allindic2 
keep if allindic2>=2
keep if curunschedprin!=. & curunschedprin!=0
so dosname masterloanidtrepp distdate
collapse (last) curunschedprin distdate,by(dosname masterloanidtrepp)
drop distdate
ren curunschedprin curunschedprin_new
sa `multppdpo',replace

u `main',clear
merge m:1 dosname masterloanidtrepp using `multppdpo'
drop if _merge==2
replace curunschedprin_keep=curunschedprin_new if _merge==3
drop _merge
drop curunschedprin_new
foreach var of varlist liqproceeds liqexpense realizedloss {
	egen max`var'=max(`var'),by(dosname masterloanidtrepp)
	drop `var'
	ren max`var' `var'
}
gen lastbeginbal=beginbal if distdate==maxdate
egen maxlastbegin=max(lastbeginbal),by(dosname masterloanidtrepp)
gen liqsize=liqproceeds/maxdisp
gen indic=1 if liqproceeds>=curunschedprin_keep & liqproceeds!=. & curunschedprin_keep!=. & liqproceeds!=0 & curunschedprin_keep!=0
replace indic=0 if liqproceeds<curunschedprin_keep & liqproceeds!=. & curunschedprin_keep!=. & liqproceeds!=0 & curunschedprin_keep!=0
winsor2 curunschedprin_keep,cuts(1 99) replace
gen unschedsize=curunschedprin_keep/maxdisp
replace unschedsize=(curunschedprin_keep/maxlastbegin) if unschedsize==. & (maxdisp==0 | maxdisp==.) & (maxlastbegin!=0  & maxlastbegin!=.)
replace unschedsize=((liqproceeds-liqexpense)/maxdisp) if unschedsize==. & liqproceeds!=. & liqexpense!=. & liqproceeds!=0 & liqexpense!=0 
replace unschedsize=((liqproceeds-liqexpense)/maxlastbegin) if unschedsize==. & liqproceeds!=. & liqexpense!=. & liqproceeds!=0 & liqexpense!=0  & (maxdisp==0 | maxdisp==.) & (maxlastbegin!=0  & maxlastbegin!=.)
winsor2 unschedsize,cuts(1 99) replace
gen dpopp_indic_base=1 if (firstprepaycd==6 | (lastworkoutmethod==6 & (firstprepaycd==3 | firstprepaycd==7)) )
gen dpopp_indic=1 if (firstprepaycd==6 | (lastworkoutmethod==6 & (firstprepaycd==3 | firstprepaycd==7))) & unschedsize>0 & unschedsize!=. & unschedsize<=0.8  
gen dpopp_indic2=1 if (firstprepaycd==6 | (lastworkoutmethod==6 & (firstprepaycd==3 | firstprepaycd==7))) & unschedsize>=.10 & unschedsize!=. 
gen dpopp_indic3=1 if (firstprepaycd==6 | (lastworkoutmethod==6 & (firstprepaycd==3 | firstprepaycd==7))) & unschedsize!=. & unschedsize<=0.8  & unschedsize>=0.10
foreach var of varlist dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 {
    replace `var'=. if evertransfer!=1
}
foreach var of varlist dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3{
	egen max`var'=max(`var'),by(dosname masterloanidtrepp)
	drop `var'
	ren max`var' `var'
}
sa `main',replace

gen foreclose_indic=1 if lastworkoutmethod==2 | (lastworkoutmethod==7 & secondtolastwomethod==2)
replace foreclose_indic=. if dpopp_indic_base==1 | dpopp_indic==1 | dpopp_indic2==1 | dpopp_indic3==1
foreach var of varlist foreclose_indic {
    replace `var'=. if evertransfer!=1
}
egen maxforeclose_indic=max(foreclose_indic),by(dosname masterloanidtrepp)
order dosname masterloanidtrepp distdate remterm workoutcode foreclose_indic maxforeclose_indic lastworkoutmethod secondtolastwomethod curunschedprin curunschedprin_keep prepaycd lastprepaycd
sa `main',replace


tempfile multfc
keep if maxforeclose_indic==1
gen indic2=1 if curunschedprin!=. & curunschedprin!=0
egen allindic2=sum(indic2),by(dosname masterloanidtrepp)
order dosname masterloanidtrepp distdate curunschedprin curunschedprin_keep remterm allindic2 
keep if allindic2>=2
keep if curunschedprin!=. & curunschedprin!=0
so dosname masterloanidtrepp distdate
collapse (last) curunschedprin distdate,by(dosname masterloanidtrepp)
drop distdate
ren curunschedprin curunschedprin_new
winsor2 curunschedprin_new,cuts(1 99) replace
sa `multfc',replace


u `main',clear
merge m:1 dosname masterloanidtrepp using `multfc'
drop if _merge==2
replace curunschedprin_keep=curunschedprin_new if _merge==3 & maxforeclose_indic==1
drop curunschedprin_new
gen unschedsize_new=curunschedprin_keep/maxdisp
replace unschedsize_new=(curunschedprin_keep/maxlastbegin) if unschedsize==. & (maxdisp==0 | maxdisp==.) & (maxlastbegin!=0  & maxlastbegin!=.)
replace unschedsize_new=((liqproceeds-liqexpense)/maxdisp) if unschedsize==. & liqproceeds!=. & liqexpense!=. & liqproceeds!=0 & liqexpense!=0 
replace unschedsize_new=((liqproceeds-liqexpense)/maxlastbegin) if unschedsize==. & liqproceeds!=. & liqexpense!=. & liqproceeds!=0 & liqexpense!=0  & (maxdisp==0 | maxdisp==.) & (maxlastbegin!=0  & maxlastbegin!=.)
winsor2 unschedsize_new,cuts(1 99) replace
drop unschedsize
ren unschedsize_new unschedsize
drop _merge
sa `main',replace


tempfile fclosedates
keep if maxforeclose_indic==1
so dosname masterloanidtrepp distdate
by dosname masterloanidtrepp: gen foreclosedate=distdate if curunschedprin==curunschedprin_keep & remterm==. & remterm[_n-1]!=.
form foreclosedate %td
collapse (max) foreclosedate,by(dosname masterloanidtrepp)
sa `fclosedates',replace

u `main',clear
merge m:1 dosname masterloanidtrepp using `fclosedates'
drop if _merge==2
drop _merge
gen mydispositiontype=dispositiontype
replace mydispositiontype="Impaired Loan Payoff" if dispositiontype=="impaired loan payoff" | dispositiontype=="Impaired loan payoff"
replace mydispositiontype="Loss" if dispositiontype=="LOSS" | dispositiontype=="loss"
replace mydispositiontype="Modeling Loan" if dispositiontype=="modeling loan" 
replace mydispositiontype="Paid Post Maturity" if dispositiontype=="Paid post Maturity" | dispositiontype=="paid post maturity" 
replace mydispositiontype="Paid at Maturity" if dispositiontype=="paid at maturity"
replace mydispositiontype="Prepay During Open" if dispositiontype=="prepay during open" | dispositiontype=="Prepay during Open" 
replace mydispositiontype="Prepay with Premium" if dispositiontype=="prepay with premium" 
replace mydispositiontype="Prepay with YM" if dispositiontype=="prepay with ym" | dispositiontype=="Prepay with Ym" 
replace mydispositiontype="Under Investigation" if dispositiontype=="under investigation" 
replace mydispositiontype="Explanation Unavailable" if dispositiontype=="explanation unavailable" 
encode mydispositiontype,g(disptypen)
egen lastnmdisptypedate=max(distdate) if disptypen!=.,by(dosname masterloanidtrepp)
form lastnmdisptypedate %td
gen lastdisptypen=disptypen if distdate==lastnmdisptypedate
egen maxlastdisptypen=max(lastdisptypen),by(dosname masterloanidtrepp)
drop lastdisptypen lastnmdisptypedate
ren maxlastdisptypen lastdisptypen
gen fullpay_from_disposition=1 if (lastdisptypen==5) | (lastdisptypen==6) | (lastdisptypen==7) | (lastdisptypen==8) | (lastdisptypen==9) | (lastdisptypen==3 & unschedsize>=.95 & unschedsize!=.)
gen full_payoff=1 if (lastworkoutmethod==11 | lastprepaycd==5 | lastprepaycd==2 | (unschedsize>=.95 & unschedsize!=.))
replace full_payoff=0 if (lastworkoutmethod!=11 & lastprepaycd!=5 & lastprepaycd!=2 & lastworkoutmethod!=. & lastprepaycd!=. & unschedsize<.95)
replace full_payoff=0 if full_payoff==1 & dpopp_indic_base==1
gen full_payoff2=1 if (lastworkoutmethod==11 | lastprepaycd==5 | lastprepaycd==2 | (unschedsize>=.95 & unschedsize!=.))
replace full_payoff2=1 if full_payoff2==. & fullpay_from_disposition==1
replace full_payoff2=0 if (lastworkoutmethod!=11 & lastprepaycd!=5 & lastprepaycd!=2 & lastworkoutmethod!=. & lastprepaycd!=. & unschedsize<.95 & fullpay_from_disposition!=1)
replace full_payoff2=0 if full_payoff2==1 & dpopp_indic_base==1
gen transfer_fullpay=1 if transfer==1 & full_payoff==1
gen fullpay_firstspcsrvtransfer=firstspcsrvtransfer if transfer_fullpay==1
replace transfer_fullpay=0 if transfer_fullpay==.
replace transfer_fullpay=. if transfer==.
gen transfer_fullpay2=1 if transfer==1 & full_payoff2==1
replace transfer_fullpay2=0 if transfer_fullpay2==.
replace transfer_fullpay2=. if transfer==.
gen dlq60 = 1 if dlqstatuscd=="2" | dlqstatuscd=="3"
egen everdlq60=max(dlq60),by(dosname masterloanidtrepp)
replace everdlq60=0 if everdlq60==.
gen transfer_nodlq2=1 if transfer==1 & everdlq60==0
replace transfer_nodlq2=0 if transfer_nodlq2==.
replace transfer_nodlq2=. if transfer==.
egen firstdlqdate=min(distdate) if dlq60==1,by(dosname masterloanidtrepp)
form firstdlqdate %td
gen firstdlq_year=year(firstdlqdate)
gen firstdlq_month=month(firstdlqdate)
gen justdlq60 = 1 if distyear==firstdlq_year & distmonth==firstdlq_month
replace justdlq60=0 if justdlq60==.
replace justdlq60=. if firstdlqdate!=. & ((distyear==firstdlq_year & distmonth>firstdlq_month) | (distyear>firstdlq_year))
egen lastdlqdate=max(distdate) if dlq60==1,by(dosname masterloanidtrepp)
form lastdlqdate %td
gen morethanonedlq=1 if firstdlqdate!=lastdlqdate
gen dlqstcd_indicator=1 if dlqstatuscd!=""
egen maxdlqstcd_indicator=max(dlqstcd_indicator),by(dosname masterloanidtrepp)

gen yearmonth=ym(distyear,distmonth)
gen post=1 if yearmonth>=597 & yearmonth!=.
replace post=0 if yearmonth<597 & yearmonth!=.


*The code on lines 207-258 accomplishes the following:;
*	1. Begins to construct indicators for control and treatment groups (some of these are never used in the analysis);
*	2. Creates a new interim dataset and drops some observations that are never used in order to shrink data in memory;

gen mydlqstatuscd=dlqstatuscd
replace mydlqstatuscd="A" if dlqstatuscd=="A." | dlqstatuscd=="a" 
drop dlqstatuscd
ren mydlqstatuscd dlqstatuscd
egen evertransfer_pre=max(transfer) if post==0,by(dosname masterloanidtrepp)
egen maxevertransfer_pre=max(evertransfer_pre),by(dosname masterloanidtrepp)
egen evertransfer_post=max(transfer) if post==1,by(dosname masterloanidtrepp)
egen maxevertransfer_post=max(evertransfer_post),by(dosname masterloanidtrepp)
drop evertransfer_pre evertransfer_post
ren maxevertransfer_pre evertransfer_pre
ren maxevertransfer_post evertransfer_post
gen noncurrent1=1 if (dlqstatuscd=="1" | dlqstatuscd=="2" | dlqstatuscd=="B") & post==0
replace noncurrent1=0 if dlqstatuscd=="0" & post==0
replace noncurrent1=. if evertransfer_pre==1
egen maxnoncurrent1=max(noncurrent1),by(dosname masterloanidtrepp)
drop noncurrent1
ren maxnoncurrent1 noncurrent1
gen noncurrent2=1 if (dlqstatuscd=="1" | dlqstatuscd=="2") & post==0
replace noncurrent2=0 if dlqstatuscd=="0" & post==0
replace noncurrent2=. if evertransfer_pre==1
egen maxnoncurrent2=max(noncurrent2),by(dosname masterloanidtrepp)
drop noncurrent2
ren maxnoncurrent2 noncurrent2
gen noncurrent3=1 if (dlqstatuscd=="1" | dlqstatuscd=="2" | dlqstatuscd=="3") & post==0
replace noncurrent3=0 if dlqstatuscd=="0"  & post==0
replace noncurrent3=. if evertransfer_pre==1
egen maxnoncurrent3=max(noncurrent3),by(dosname masterloanidtrepp)
drop noncurrent3
ren maxnoncurrent3 noncurrent3
gen noncurrent4=1 if (dlqstatuscd=="1" | dlqstatuscd=="2" | dlqstatuscd=="3") & post==0
replace noncurrent4=0 if dlqstatuscd=="0"  & post==0
replace noncurrent4=. if evertransfer_pre==1
egen totalnoncurrent4=sum(noncurrent4) if post==0 & noncurrent4!=.,by(dosname masterloanidtrepp)
gen noncurrent4indic=1 if totalnoncurrent4>=2 & totalnoncurrent4!=. & post==0
replace noncurrent4indic=0 if totalnoncurrent4<2 & totalnoncurrent4!=. & post==0
replace noncurrent4indic=. if totalnoncurrent4==.
egen maxnoncurrent4=max(noncurrent4indic),by(dosname masterloanidtrepp)
drop noncurrent4 noncurrent4indic
ren maxnoncurrent4 noncurrent4
assert noncurrent4==0 if noncurrent3==0
assert noncurrent3==1 if noncurrent4==1
gen noncurrent5=1 if (dlqstatuscd=="1" | dlqstatuscd=="2" | dlqstatuscd=="3") & (post==0 & distym<595)
replace noncurrent5=0 if (dlqstatuscd=="0" | dlqstatuscd=="A" | dlqstatuscd=="B") & (post==0 & distym<595)
egen maxnoncurrent5=max(noncurrent5),by(dosname masterloanidtrepp)
drop noncurrent5
ren maxnoncurrent5 noncurrent5
sa `main',replace

sa step1data_interim00,replace 

keep if yearmonth>=524 & yearmonth<=668
sa `main',replace



*The code on lines 266-602 accomplishes the following:;
*	1. Merges and cleans deal characteristics, including special and master servicer names;
*	2. Creates the DPO indicators;

tempfile dealchars
u "<directory>",clear
forv i=2001(1)2015 {
	ap using "<directory>",force
}
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ap using "<directory>",force
ren dealname dosname
ren tapedate distdate
foreach var of varlist distdate closingdate {
	tostring `var',g(c`var')
	gen `var'2=date(c`var',"YMD")
	form `var'2 %td
	drop `var' c`var'
	ren `var'2 `var'
}
keep dosname dealtype specialservicer masterservicer
collapse (last) specialservicer dealtype masterservicer,by(dosname)
drop if dealtype=="Agency CMBS" | dealtype=="Agency Pool" | dealtype=="FHA" | dealtype=="Agency"
drop if specialservicer=="Federal Home Loan Mortgage Corp" | specialservicer=="Freddie Mac"
drop if specialservicer=="None"
gen mydealtype="Single/Large" if dealtype=="Large Loan" | dealtype=="SnglAsset/Borr"
replace mydealtype="Conduit" if dealtype=="Conduit"
replace mydealtype="Other" if dealtype=="Miscellaneous" | dealtype=="Single Family Rental" | dealtype=="Small Loan"
replace mydealtype="Other" if mydealtype==""
egen dealtypeid=group(mydealtype)
gen myss=specialservicer
replace myss="AMRESCO Services" if specialservicer=="AMRESCO Services" | specialservicer=="AMRESCO Management Inc" | specialservicer=="Amresco"
replace myss="Archon" if specialservicer=="Archon Group, L.P."
replace myss="CWCapital Asset Management" if specialservicer=="CWCapital Asset Mgt/NCB,FSB" | specialservicer=="CWCapital Investments" | specialservicer=="CWCapital Asset Management LCC." | specialservicer=="CWCapital Asset Mgmt LLC" | specialservicer=="CWCapital LLC"
replace myss="Clarion" if specialservicer=="Clarion Partners LLC"
replace myss="Hudson Advisors LLC" if specialservicer=="Hudson Americas LLC" | specialservicer=="Hudson Americas LP"
replace myss="KeyBank" if specialservicer=="KeyBank National Association" | specialservicer=="KeyCorp Real Estate Capital" | specialservicer=="Key Corp"
replace myss="LNR Partners" if specialservicer=="LNR Partners, Inc." | specialservicer=="LNR Partners, Inc./NCB,FSB" | specialservicer=="LNR Partners, LLC." | specialservicer=="LNR Property Corporation" | specialservicer=="LNR Partners, LLC/NCB"
replace myss="Lend Lease" if specialservicer=="Lend Lease RE Investments"
replace myss="Midland" if specialservicer=="Midland/NCB" | specialservicer=="Midland/NCB,FSB"
replace myss="NCB" if specialservicer=="NCB, FSB" | specialservicer=="NCB, FSB/CWCapital" | specialservicer=="NCB, FSB/LNR Partners, Inc." | specialservicer=="NCB, FSB/Torchlight" | specialservicer=="NCB,FSB/C-III Asset Management" 
replace myss="Rialto Capital Advisors, LLC" if specialservicer=="Rialto Capital Advisors/NCB" | specialservicer=="Rialto Capital Advisors/NCB,FSB"
replace myss="Situs Holdings, LLC" if specialservicer=="Situs Asset Management Limited"
replace myss="C-III Asset Management LLC" if specialservicer=="C-III Asset Management LLC/NCB"
replace myss="CWCapital Asset Management" if specialservicer=="CWCapital Asset Management/NCB"
replace myss="Capmark Finance" if specialservicer=="CapMark" | specialservicer=="Capmark"
replace myss="GMAC" if specialservicer=="GMAC Commercial Mortgage Serv." | strmatch(specialservicer,"GMAC*")
replace myss="LNR Partners" if specialservicer=="LNR Partners, LLC/NCB";
replace myss="Midland" if specialservicer=="Midland/Wells Fargo/AEGON" | specialservicer=="midland" | strmatch(specialservicer,"Midland*")
replace myss="Rialto Capital Advisors, LLC" if strmatch(specialservicer,"Rialto*") 
replace myss="Strategic Asset Services, LLC" if specialservicer=="Strategic Asset Services"
replace myss="TriMont Real Estate Advisors" if strmatch(specialservicer,"TriMont*") | strmatch(specialservicer,"Trimont*") 
replace myss="Wells Fargo" if strmatch(specialservicer,"Wells Fargo*")
replace myss="Cohen" if specialservicer=="Cohen Financial" | specialservicer=="Cohen Financial Services LLC"
replace myss="ACRE" if specialservicer=="Ares Commercial R.E. Serv. LLC"
replace myss="CRIIMI MAE" if specialservicer=="Criimi Mae"
replace myss="Bank of America" if specialservicer=="Banc of America"
replace myss="Falcon" if specialservicer=="Falcon Financial LLC"
replace myss="Franchise Fin. Corp. of America" if strmatch(specialservicer,"Franchise Finance Corporation of Amer*")
replace myss="GE Capital Realty Group" if specialservicer=="GE Capital Realty Group, Inc."
replace myss="Hudson Advisors LLC" if strmatch(specialservicer,"Hudson*")
replace myss="JE Roberts" if specialservicer=="JE Robert Company"
replace myss="Lennar" if strmatch(specialservicer,"Lennar*")
replace myss="Midland" if strmatch(specialservicer,"Midland*")
replace myss="Torchlight" if strmatch(specialservicer,"Torchlight*")
replace myss="ORIX" if strmatch(specialservicer,"ORIX*") | strmatch(specialservicer,"Orix*")
replace myss="Pacific Life" if strmatch(specialservicer,"Pacific Life*") | strmatch(specialservicer,"Pacific life*")
replace myss="WaMu" if strmatch(specialservicer,"Washington Mutual*")
gen myms=masterservicer
replace myms="Bankers Trust" if strmatch(masterservicer,"Bankers Trust*")
replace myms="Berkadia" if strmatch(masterservicer,"Berkadia*")
replace myms="CapMark" if strmatch(masterservicer,"CapMark*") | strmatch(masterservicer,"Capmark*")  
replace myms="First Union" if strmatch(masterservicer,"First Union*")
replace myms="GMAC" if strmatch(masterservicer,"GMAC*")
replace myms="NCB" if strmatch(masterservicer,"NCB*")
replace myms="KeyBank" if strmatch(masterservicer,"KeyBank*")
replace myms="Midland" if strmatch(masterservicer,"Midland*")
replace myms="KeyCorp" if strmatch(masterservicer,"Key Corp*") | strmatch(masterservicer,"KeyCorp*")
replace myms="MassHousing" if strmatch(masterservicer,"MassHousing*") | strmatch(masterservicer,"Mass Housing*")
replace myms="NCB" if strmatch(masterservicer,"NCB*")
replace myms="Prudential" if strmatch(masterservicer,"Prudential*")
replace myms="Wells Fargo" if strmatch(masterservicer,"Wells Fargo*")
egen ssid=group(myss)
egen msid=group(myms)
keep dosname mydealtype myss ssid myms msid dealtypeid
so dosname
sa `dealchars',replace

u `main',clear
merge m:1 dosname using `dealchars'
drop if _merge==2
drop _merge
compress
sa `main',replace

sa step1data_interim0,replace

u step1data_interim0,clear
sa `main',replace

tempfile ppdpodates
keep myss dosname masterloanidtrepp firstprepaydate dpopp_indic* lastworkoutmethod firstworkoutmethod 
gen firstppyear=year(firstprepaydate)
gen firstppmonth=month(firstprepaydate)
ren firstppyear distyear
ren firstppmonth distmonth
sa `ppdpodates',replace

tempfile onlydates
u `main',clear
keep myss distyear distmonth dosname masterloanidtrepp
so myss distyear distmonth
drop if myss==""
sa `onlydates',replace

tempfile dpopp_ss
u `ppdpodates',clear
keep dpopp_indic_base myss distyear distmonth dosname masterloanidtrepp
drop if myss==""
keep if dpopp_indic_base!=.
collapse (max) distyear distmonth dpopp_indic_base (last) myss,by(dosname masterloanidtrepp)
drop if distyear==.
merge 1:1 myss dosname masterloanidtrepp distyear distmonth using `onlydates'
egen maxmerge=max(_merge),by(dosname masterloanidtrepp)
collapse (sum) dpopp_indic_base,by(myss distyear distmonth)
gen distym=ym(distyear,distmonth)
so myss distyear distmonth
by myss: gen cum_dpo=sum(dpopp_indic_base)
rangestat (sum) dpopp_indic_base, interval(distym -37 -1) by(myss)
ren dpopp_indic_base_sum cum_dpo_3yr
rangestat (sum) dpopp_indic_base, interval(distym -61 -1) by(myss)
ren dpopp_indic_base_sum cum_dpo_5yr
rangestat (sum) dpopp_indic_base, interval(distym -13 -1) by(myss)
ren dpopp_indic_base_sum cum_dpo_1yr
drop distym
tempfile ss_cumulative
drop dpopp_indic_base
so myss distyear distmonth
by myss: gen lag_cdpo=cum_dpo[_n-1]
drop cum_dpo
ren lag_cdpo cum_dpo
sa `ss_cumulative',replace

tempfile inservicing dpobyssdate avgvolume
u `main',clear
gen inservicing=1 if workoutcode!=. & workoutcode!=8 & workoutcode!=9 & workoutcode!=13
replace inservicing=0 if workoutcode==. | workoutcode==8 | workoutcode==9 | workoutcode==13
replace inservicing=0 if beginbal==0
drop if myss==""
collapse (sum) inservicing,by(myss distyear distmonth)
sa `inservicing',replace

u `ppdpodates',clear
keep if dpopp_indic_base!=.
drop if myss==""
collapse (max) distyear distmonth dpopp_indic_base (last) myss,by(dosname masterloanidtrepp)
drop if distyear==.
collapse (sum) dpopp_indic_base,by(myss distyear distmonth)
sa `dpobyssdate',replace

u `inservicing',clear
merge 1:1 myss distyear distmonth using `dpobyssdate'
drop _merge
replace dpopp_indic_base=0 if dpopp_indic_base==.
so myss distyear distmonth
gen dpo_volume=dpopp_indic_base/inservicing
replace dpo_volume=0 if dpo_volume==.
so myss distyear distmonth
by myss: gen avg_dpo_volume=sum(dpo_volume)/sum(dpo_volume < .)
so myss distyear distmonth
by myss: gen lag_avg=avg_dpo_volume[_n-1]
gen distym=ym(distyear,distmonth)
rangestat (mean) dpo_volume, interval(distym -37 -1) by(myss)
ren dpo_volume_mean avg_dpo_volume_3yr
rangestat (mean) dpo_volume, interval(distym -61 -1) by(myss)
ren dpo_volume_mean avg_dpo_volume_5yr
rangestat (mean) dpo_volume, interval(distym -13 -1) by(myss)
ren dpo_volume_mean avg_dpo_volume_1yr
drop avg_dpo_volume dpopp_indic_base dpo_volume distym
ren lag_avg avg_dpo_volume
sa `avgvolume',replace

tempfile onlydates
u `main',clear
keep myss distyear distmonth dosname masterloanidtrepp
so myss distyear distmonth
sa `onlydates',replace

tempfile dpopp_ss
u `ppdpodates',clear
keep dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base myss distyear distmonth
collapse (max) dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base,by(myss distyear distmonth)
merge 1:m myss distyear distmonth using `onlydates'
collapse (max) dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base,by(myss distyear distmonth)
drop if myss==""
so myss distyear distmonth
ren dpopp_indic dpopp_indic_ss1
ren dpopp_indic2 dpopp_indic_ss2 
ren dpopp_indic3 dpopp_indic_ss3
ren dpopp_indic_base dpopp_indic_ss0
forv j=0(1)3 {

	forv i=1(1)24 {
		by myss: gen l`i'_ssd=dpopp_indic_ss`j'[_n-`i']
	}
	
	
	forv i=1(1)24 {
		by myss: gen ld`i'_ssd=dpopp_indic_ss`j'[_n+`i']
	}
	

	by myss: gen dspp`j'_4t12=1 if l4_ssd==1 | l5_ssd==1 | l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1 | l11_ssd==1 | l12_ssd==1
	by myss: gen dspp`j'_5t12=1 if l5_ssd==1 | l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1 | l11_ssd==1 | l12_ssd==1

	by myss: gen dspp`j'_7t10=1 if l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1
	by myss: gen dspp`j'_6t9=1 if l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1
	by myss: gen dspp`j'_5t8=1 if l5_ssd==1 | l6_ssd==1 | l7_ssd==1 | l8_ssd==1
	by myss: gen dspp`j'_4t7=1 if l4_ssd==1 | l5_ssd==1 | l6_ssd==1 | l7_ssd==1
	by myss: gen dspp`j'_3t6=1 if l3_ssd==1 | l4_ssd==1 | l5_ssd==1 | l6_ssd==1
	
	by myss: gen dspp`j'_5t11=1 if l5_ssd==1 | l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1 | l11_ssd==1
	by myss: gen dspp`j'_6t12=1 if l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1 | l11_ssd==1 | l12_ssd==1
	by myss: gen dspp`j'_12t18=1 if l12_ssd==1 | l13_ssd==1 | l14_ssd==1 | l15_ssd==1 | l16_ssd==1 | l17_ssd==1 | l18_ssd==1
	
	by myss: gen dspp`j'_1t12=1 if l1_ssd==1 | l2_ssd==1 | l3_ssd==1 | l4_ssd==1 | l5_ssd==1 | l6_ssd==1 | l7_ssd==1 | l8_ssd==1 | l9_ssd==1 | l10_ssd==1 | l11_ssd==1 | l12_ssd==1
	
	drop l1_ssd-ld24_ssd
}
drop dpopp_indic_ss0 dpopp_indic_ss1 dpopp_indic_ss2 dpopp_indic_ss3
so myss distyear distmonth
foreach var of varlist dspp0_4t12-dspp3_1t12 {
	replace `var'=0 if `var'==.
}
sa `dpopp_ss',replace

u `main',clear
so dosname masterloanidtrepp distdate
merge m:1 myss distyear distmonth using `dpopp_ss'
drop if _merge==2
drop _merge
sa `main',replace
merge m:1 myss distyear distmonth using `ss_cumulative'
drop if _merge==2
drop _merge
merge m:1 myss distyear distmonth using `avgvolume'
drop if _merge==2
drop _merge
sa `main',replace

tempfile capacity capacity2
collapse (max) avg_dpo_volume cum_dpo inservicing,by(myss distyear distmonth)
drop if myss==""
gen distym=ym(distyear, distmonth)
so myss distyear distmonth
by myss: gen avg_inservicing_full=sum(inservicing)/sum(inservicing < .)
gen high_inservicing_full=1 if inservicing>avg_inservicing_full & avg_inservicing_full!=. & inservicing!=.
replace high_inservicing_full=0 if inservicing<=avg_inservicing_full & avg_inservicing_full!=. & inservicing!=.
rangestat (mean) inservicing, interval(distym -13 -1) by(myss)
ren inservicing_mean avg_inservicing_1yr
gen high_inservicing=1 if inservicing>avg_inservicing_1yr & avg_inservicing_1yr!=. & inservicing!=.
replace high_inservicing=0 if inservicing<=avg_inservicing_1yr & avg_inservicing_1yr!=. & inservicing!=.
rangestat (median) avg_dpo_volume, interval(distym -13 -1)
sa `capacity',replace
collapse (max) cum_dpo avg_dpo_volume inservicing,by(myss distym)
egen ssid=group(myss)
reshape wide cum_dpo avg_dpo_volume myss inservicing,i(distym) j(ssid)
egen median_cumdpo=rowmedian(cum_dpo*)
egen median_avgvolume=rowmedian(avg_dpo_volume*)
keep distym median_cumdpo median_avgvolume
so distym
foreach var of varlist median_cumdpo median_avgvolume {
    gen l_`var'=`var'[_n-1]
}
drop median_cumdpo median_avgvolume
ren l_median_cumdpo median_cumdpo
ren l_median_avgvolume median_avgvolume
keep distym median_cumdpo median_avgvolume
sa `capacity2',replace

u `capacity',clear
merge m:1 distym using `capacity2'
drop _merge

gen high_low=1 if high_inservicing==0 & (avg_dpo_volume > avg_dpo_volume_median) & avg_dpo_volume!=. & avg_dpo_volume_median!=. & high_inservicing!=.
replace high_low=0 if high_inservicing==1 | (avg_dpo_volume <= avg_dpo_volume_median) & avg_dpo_volume!=. & avg_dpo_volume_median!=. & high_inservicing!=.
keep myss distyear distmonth high_low
sa `capacity',replace

u `main',clear
merge m:1 myss distyear distmonth using `capacity'
drop if _merge==2
drop _merge
sa `main',replace


tempfile dpodata
u step1data_interim0,clear
keep myss dosname masterloanidtrepp firstprepaydate dpopp_indic* lastworkoutmethod firstworkoutmethod 
gen firstppyear=year(firstprepaydate)
gen firstppmonth=month(firstprepaydate)
ren firstppyear distyear
ren firstppmonth distmonth
ren firstprepaydate distdate
keep if dpopp_indic_base==1 | dpopp_indic==1 | dpopp_indic2==1 | dpopp_indic3==1
collapse (max) distyear distmonth distdate dpopp_indic*,by(dosname masterloanidtrepp)
drop if distdate==.
gen dpopp_indic_basedate=distdate if dpopp_indic_base==1
gen dpopp_indicdate=distdate if dpopp_indic==1
gen dpopp_indic2date=distdate if dpopp_indic2==1
gen dpopp_indic3date=distdate if dpopp_indic3==1
form dpopp_indic_basedate dpopp_indicdate dpopp_indic2date dpopp_indic3date %td
drop distyear distmonth
sa `dpodata',replace 

u `main',clear
drop dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base 
merge 1:1 dosname masterloanidtrepp distdate using `dpodata'
drop if _merge==2
drop _merge
foreach var of varlist dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base {
	egen max`var'=max(`var'),by(dosname masterloanidtrepp)
	drop `var'
	ren max`var' `var'
}
foreach var of varlist dpopp_indic_basedate dpopp_indicdate dpopp_indic2date dpopp_indic3date {
	egen max`var'=max(`var'),by(dosname masterloanidtrepp)
	drop `var'
	ren max`var' `var'
}
foreach var of var dpopp_indic dpopp_indic2 dpopp_indic3 dpopp_indic_base {
	replace `var'=0 if `var'==.
}


*The code on lines 612-854 accomplishes the following:;
*	1. Does additional data cleaning;
*	2. Constructs fixed effects;
*	3. Constructs various data filters (never used in the analysis);
*	4. Winsorizes;
*	5. Finishes constructing indicators for control and treatment groups;
*	6. Saves the final dataset used to run the regressions;

drop if remterm==.
egen maxmsa=max(msa),by(dosname masterloanidtrepp)
drop msa
ren maxmsa msa
drop if msa==.
sa `main',replace

tempfile lendercounts
keep originator dosname masterloanidtrepp post origyear distyear
keep if post==0
collapse (max) origyear,by(originator dosname masterloanidtrepp)
egen lcount=total(1),by(originator)
drop if originator==""
collapse (max) lcount,by(originator)
keep originator lcount
sa `lendercounts',replace

u `main',clear
merge m:1 originator using `lendercounts'
drop if _merge==2
drop _merge
sa `main',replace


tempfile sscounts
collapse (last) beginbal, by(myss dosname masterloanidtrepp)
drop if myss==""
so myss dosname masterloanidtrepp
by myss: gen loancounter=_n
egen maxloancount=max(loancounter),by(myss)
drop loancounter
ren maxloancount loancountss
collapse (max) loancountss,by(myss dosname)
by myss: gen dealcounter=_n
collapse (max) dealcounter loancountss,by(myss)
gen dcountss_5=1 if dealcounter>=5
gen dcountss_2=1 if dealcounter>=2
gen lcountss_100=1 if loancountss>=100
gen lcountss_50=1 if loancountss>=50
sa `sscounts',replace

u `main',clear
merge m:1 myss using `sscounts'
drop if _merge==2
drop _merge
sa `main',replace

tempfile msaymcounts
keep dosname masterloanidtrepp msa yearmonth
so msa yearmonth
by msa yearmonth: gen indic=1
collapse (sum) indic,by(msa yearmonth)
gen maxcount_25=1 if indic>=25 & indic!=.
gen maxcount_100=1 if indic>=100 & indic!=.
keep msa yearmonth maxcount_* 
sa `msaymcounts',replace

u `main',clear
so msa yearmonth
merge m:1 msa yearmonth using `msaymcounts'
drop if _merge==2
drop _merge
egen ssmsa=group(myss msa)
egen msaym=group(msa yearmonth)
drop if originator==""
sa `main',replace

tempfile ssmsacounts
keep dosname masterloanidtrepp msa myss
so myss msa
duplicates drop dosname masterloanidtrepp msa myss,force
by myss msa: gen indic=1
collapse (sum) indic,by(myss msa)
ren indic ssmsa_lcount
sa `ssmsacounts',replace

u `main',clear
merge m:1 myss msa using `ssmsacounts'
drop if _merge==2
drop _merge
egen originatorid=group(originator)
sa `main',replace
gen anydpopp_indic=1 if dpopp_indic_base==1
egen maxindic=max(anydpopp_indic),by(dosname masterloanidtrepp)
gen nouse2=1 if maxindic==1
replace nouse2=0 if maxindic!=1
drop anydpopp_indic
ren maxindic anydpopp_indic
egen maxdpo_pre=max(anydpopp_indic) if post==0,by(myss)
egen maxdpo_post=max(anydpopp_indic) if post==1,by(myss)
egen maxssdpo_pre=max(maxdpo_pre),by(myss)
egen maxssdpo_post=max(maxdpo_post),by(myss)
keep if maxssdpo_pre==1 & maxssdpo_post==1
sa step1data_interim1,replace


u step1data_interim1,clear
drop indic
keep if yearmonth>=572 & yearmonth<=620
drop if origyear>2007
drop if origyear<=2007 & year(firstdistdate)>=2009
sa `main',replace

tempfile loancounts
so dosname masterloanidtrepp distdate
by dosname masterloanidtrepp: gen counter=_n
collapse (max) counter,by(dosname masterloanidtrepp)
keep if counter>=12
sa `loancounts',replace

u `main',clear
merge m:1 dosname masterloanidtrepp using `loancounts'
keep if _merge==3
drop _merge

foreach var of varlist orig_ltv securwac securocc orig_dscrncf2 orig_dscrnoi2 age balratio ltv occrate dscrncf dscrnoi {
	winsor2 `var',replace cuts(1 99)
}
egen ssmsaym=group(ssid msa ym)
egen ssym=group(ssid ym)
gen distq=quarter(distdate)
gen distyq=yq(distyear,distq)
egen msayq=group(msa distyq)
gen origq=quarter(origdate)
gen origyq=yq(origyear,origq)
gen treated_dlq=1 if noncurrent5==0
replace treated_dlq=0 if noncurrent5==1
replace treated_dlq=0 if noncurrent5==.
sa `main',replace
gen july09_dscr=dscrnoi if distym==594
egen maxjuly09_dscr=max(july09_dscr),by(dosname masterloanidtrepp)
gen treated_dscr=1 if maxjuly09_dscr>1.25 & maxjuly09_dscr!=.
replace treated_dscr=0 if maxjuly09_dscr<=1.25 & maxjuly09_dscr!=.
egen pre_avgdscr=mean(dscrnoi) if post==0,by(dosname masterloanidtrepp)
egen maxpre_avgdscr=max(pre_avgdscr),by(dosname masterloanidtrepp)
gen treated_dscravg=1 if maxpre_avgdscr>1.25 & maxpre_avgdscr!=.
replace treated_dscravg=0 if maxpre_avgdscr<=1.25 & maxpre_avgdscr!=.
sa `main',replace

tempfile wl
keep if post==0 & distym<595
collapse (max) watchlistym, by(dosname masterloanidtrepp)
gen neverwlpre=1 if watchlistym==.
replace neverwlpre=0 if watchlistym!=.
sa `wl',replace

u `main',clear
merge m:1 dosname masterloanidtrepp using `wl'
drop if _merge==2
drop _merge
gen treated_wl=1 if neverwlpre==1 
replace treated_wl=0 if neverwlpre==0
su treated_wl,d
gen treated_dlqwl=1 if neverwlpre==1 & noncurrent5==0
replace treated_dlqwl=0 if noncurrent5==1
gen msaname="Austin" if msa==12420
replace msaname="Atlanta" if msa==12060
replace msaname="Baltimore" if msa==12580
replace msaname="Boston" if msa==14460 
replace msaname="Charlotte"  if msa==16740 
replace msaname="Chicago"  if msa==16980
replace msaname="DC"  if msa==47900
replace msaname="DalFW"  if msa==19100
replace msaname="Denver"  if msa==19740
replace msaname="Detroit"  if msa==19820
replace msaname="Houston"  if msa==26420
replace msaname="Indianapolis"  if msa==26900
replace msaname="Inland"  if msa==40140
replace msaname="Jacksonville"  if msa==27260
replace msaname="Los Angeles"  if msa==31080 | msa==31100
replace msaname="Las Vegas"  if msa==29820
replace msaname="Miami"  if msa==33100
replace msaname="NYC"  if msa==35620
replace msaname="Nashville"  if msa==34980
replace msaname="Orleans"  if msa==35380
replace msaname="Norfolk"  if msa==47260
replace msaname="Orlando"  if msa==36740
replace msaname="Phil"  if msa==37980
replace msaname="Phoenix"  if msa==38060
replace msaname="Portland"  if msa==38900
replace msaname="Raleigh"  if msa==39580
replace msaname="Richmond"  if msa==40060
replace msaname="San Diego"  if msa==41740
replace msaname="San Fran"  if msa==41860
replace msaname="San Jose"  if msa==41940
replace msaname="Seattle"  if msa==42660
replace msaname="Tampa" if msa==45300
replace msaname="Minneapolis" if msa==33460
replace msaname="Sacramento" if msa==40900
replace msaname="San Antonio" if msa==41700
replace msaname="Columbus" if msa==18140
replace msaname="Cincinnati" if msa==17140
replace msaname="Kansas City" if msa==28140
replace msaname="Cleveland" if msa==17460
gen post_treated_dlq=post*treated_dlq 
gen post_treated_wl=post*treated_wl 
gen post_treated_dlqwl=post*treated_dlqwl
gen post_hfcl=post*highfcloss
gen post_cumdpo=post*cum_dpo
gen post_cumdpo_3=post*cum_dpo_3yr
gen cumdpo_treated_wl=cum_dpo*treated_wl
gen cumdpo_treated_dlq=cum_dpo*treated_dlq
gen cumdpo_treated_wl_3=cum_dpo_3yr*treated_wl
gen cumdpo_treated_dlq_3=cum_dpo_3yr*treated_dlq
gen intxn3_cumdpo_wl=post*cum_dpo*treated_wl
gen intxn3_cumdpo_dlq=post*cum_dpo*treated_dlq
gen intxn3_cumdpo_wl_3=post*cum_dpo_3yr*treated_wl
gen intxn3_cumdpo_dlq_3=post*cum_dpo_3yr*treated_dlq
gen post_avgvolume=post*avg_dpo_volume
gen post_avgvolume_3=post*avg_dpo_volume_3yr 
gen avgvolume_treated_wl=avg_dpo_volume*treated_wl
gen avgvolume_treated_dlq=avg_dpo_volume*treated_dlq
gen avgvolume_treated_wl_3=avg_dpo_volume_3yr*treated_wl
gen avgvolume_treated_dlq_3=avg_dpo_volume_3yr*treated_dlq
gen intxn3_avgvolume_wl=post*avg_dpo_volume*treated_wl
gen intxn3_avgvolume_dlq=post*avg_dpo_volume*treated_dlq
gen intxn3_avgvolume_wl_3=post*avg_dpo_volume_3yr*treated_wl
gen intxn3_avgvolume_dlq_3=post*avg_dpo_volume_3yr*treated_dlq
foreach var of varlist dspp0_5t8 dspp0_4t7 dspp0_6t9 dspp0_7t10 dspp0_1t12 {
    gen post_`var'=post*`var'
}
foreach var of varlist dspp0_5t8 dspp0_4t7 dspp0_6t9 dspp0_7t10 dspp0_1t12 {
	gen `var'_treated_dlq=`var'*treated_dlq
	gen intxn3_`var'_dlq=post*treated_dlq*`var'
	gen `var'_treated_wl=`var'*treated_wl
	gen intxn3_`var'_wl=post*treated_wl*`var'
	gen `var'_treated_dlqwl=`var'*treated_dlqwl
	gen intxn3_`var'_dlqwl=post*treated_dlqwl*`var'
}
merge m:1 myss using dpoinwindowss
drop if _merge==2
drop _merge
gen post_dpowindow=post*dpoinwindow
gen post_dpowindow_dlq=treated_dlq*post_dpowindow
gen post_dpowindow_wl=treated_wl*post_dpowindow
gen dpowindow_wl=dpoinwindow*treated_wl
tab myss,g(ssdum)
drop ssdum1
foreach var of varlist ssdum2-ssdum13 {
    gen `var'_post=`var'*post
}
sa final_dataset,replace